﻿using System;
using System.Collections.Generic;
using System.Text;
using OpenSocialPlugin.General;
using org.opensocial.client;
using org.opensocial.data;

namespace OpenSocialPlugin.Providers
{
    public class MySpace : BaseSite, ISocialSite
    {
        OpenSocialClient client = new OpenSocialClient(new org.opensocial.client.Gadget
        {
            ConsumerSecret = "02233daae39c4ed19c754b8ae061d3ef",
            OutgoingConsumerKey = "http://www.myspace.com/483299470",
            Container = org.opensocial.client.OpenSocialProvider.MYSPACE
        });

        #region ISocialSite Members

      public string GetPersonInfo(OSP_Person person)
      {
        throw new NotImplementedException();
      }

      public string GetPhotoInfo(OSP_Photo photo)
      {
        throw new NotImplementedException();
      }

      public List<string> SearchNames { get { return null; } }
        public List<string> ExtrasNames { get { return null; } }

        public GeneralResponse Extras(string serchtxt, int type)
        {
            return new GeneralResponse();
        }

        public GeneralResponse Search(string serchtxt, int type)
        {
            return new GeneralResponse();
        }


        public GeneralResponse Home()
        {
            GeneralResponse res = new GeneralResponse();
            OSP_Person pers = Login();
            List<OSP_Person> list = GetFriends(pers);
            res.Response = list;
            res.Title = string.Format("Friends of {0} ({1})", pers, list.Count);
            return res;
        }

        public List<OSP_Person> GetFriends(OSP_Person person)
        {
            string ViewerId = person.Id; //"125996559"; // Hi5 random account/*
            client.ViewerId = person.Id;
            client.RpcEndpoint = null; // RPC is preferred because it supports batch requests; so set it to null to use REST

            // Retrieve stuff
            //System.Console.Out.WriteLine(c.listMethods());
            //Output(c.fetchActivities());
            var viewerRequest = OpenSocialClient.newFetchPersonRequest(ViewerId)// TODO: MySpace doesn't support '@me'
                .setFields("@all");
            viewerRequest.setCount(1000);
            var friendsRequest = OpenSocialClient.newFetchFriendsRequest(ViewerId);
            //.setCount(1000) // TODO: Not supported by Hi5
            //.setFields("photos");
            friendsRequest.setCount(500);
            Response<Person> friends;

            if (client.UseRpc) // Use batch
            {
                var response = new OpenSocialBatch() // TODO: Make REST support this behind the scene... Users should not have to write two implementations
                    .addRequest(viewerRequest, "viewerRequest")
                    .addRequest(friendsRequest, "friendsRequest")
                    .send(client);

                var viewer = response.getItemAsPerson("viewerRequest");
                friends = response.getItemAsPersonCollection("friendsRequest", client.UseRpc);
                friends.Insert(0, viewer);
            }
            else
            {
                var viewer = viewerRequest.requestObject<Person>(client);
                friends = friendsRequest.requestCollection<Person>(client);
                friends.Insert(0, viewer);
            }
            List<OSP_Person> persons = new List<OSP_Person>();
            foreach (var friend in friends)
            {
                persons.Add(To_Osd_Person(friend));

            }
            return persons;
        }

        public List<OSP_Album> GetAlbums(OSP_Person person)
        {
            throw new NotImplementedException();
        }

        public List<OSP_Photo> GetPhotos(OSP_Album album)
        {
            throw new NotImplementedException();
        }

        public void Init()
        {
            _capabilities = new List<CapabilitiesEnum>();
            throw new NotImplementedException();
        }

        public OSP_Person Login()
        {
            throw new NotImplementedException();
        }

        public System.Windows.Forms.Control SettingForm
        {
            get { throw new NotImplementedException(); }
        }

        public OSP_Person GetPerson(string personId)
        {
            throw new NotImplementedException();
        }

        List<CapabilitiesEnum> _capabilities;
        public List<CapabilitiesEnum> Capabilities { get { return _capabilities; } }

        #endregion
    }
}
